package com.dragon.framework.manager.factory;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.dragon.common.constant.BasicConstant;
import com.dragon.common.utils.MdcUtils;
import com.dragon.common.utils.ServletUtils;
import com.dragon.common.utils.ip.AddressUtils;
import com.dragon.common.utils.ip.IpUtils;
import com.dragon.common.utils.spring.SpringUtils;
import com.dragon.interfaces.entity.UserLoginLog;
import com.dragon.interfaces.entity.UserOperateLog;
import com.dragon.interfaces.mapper.UserLoginLogMapper;
import com.dragon.interfaces.mapper.UserOperateLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author dragon
 * @version 1.0
 * @date 2024/8/2 11:17
 */
@Slf4j
public class AsyncFactory {

    /**
     * 用户登陆异步任务
     *
     * @param account 账号
     * @param status  状态
     * @param message 消息
     * @param args    参数
     * @return TimerTask
     */
    public static TimerTask insertLoginLog(final String account, final Byte status, final String message,
                                           final Object... args) {
        final UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        // 客户端IP
        final String ip = IpUtils.getIpAddr();
        // 请求ID
        String traceId = MdcUtils.safeGenerate(BasicConstant.TRACE_ID);

        return new TimerTask() {
            @Override
            public void run() {
                // 获取客户端操作系统
                String os = userAgent.getOs().getName();
                String osVersion = userAgent.getOsVersion();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                String browserVersion = userAgent.getVersion();
                // 获取客户端引擎类型
                String engine = userAgent.getEngine().getName();
                String engineVersion = userAgent.getEngineVersion();

                // 用户登陆日志
                UserLoginLog userLoginLog = UserLoginLog
                    .builder()
                    .traceId(traceId)
                    .account(account)
                    .loginIp(ip)
                    .browser(browser + (StringUtils.isBlank(browserVersion) ? "" : " " + browserVersion))
                    .os(os + (StringUtils.isBlank(osVersion) ? "" : " " + osVersion))
                    .engine(engine + (StringUtils.isBlank(engineVersion) ? "" : " " + engineVersion))
                    .status(status)
                    .msg(message)
                    .loginTime(LocalDateTime.now())
                    .loginLocation(AddressUtils.getRealAddressByIP(ip))
                    .build();

                SpringUtils.getBean(UserLoginLogMapper.class).insert(userLoginLog);

            }
        };
    }

    /**
     * 用户操作日志记录
     *
     * @param userOperateLog 操作日志实体
     * @return TimerTask
     */
    public static TimerTask insertUserOperateLog(final UserOperateLog userOperateLog) {
        return new TimerTask() {
            @Override
            public void run() {
                userOperateLog.setOperateLocation(AddressUtils.getRealAddressByIP(userOperateLog.getRequestIp()));

                SpringUtils.getBean(UserOperateLogMapper.class).insert(userOperateLog);
            }
        };
    }

}
